<?php

class action
{
    public function index() {

        $status = null;

        extract(URLTracker::init(array(
            'status' => 1
        )));

        $goods_category = array_to_filelds_assoc('id', _model('goods_category')->getList(array('status' => $status)));
        $goods_sum = array_to_filelds_assoc('category_id', run_sql('goods', 'getAll', 'SELECT `category_id`, count(`id`) \'num\' FROM `goods` GROUP BY `category_id`'));
        foreach ($goods_category as $key => $item) {
            if (isset($goods_sum[$key])) {
                $num = intval($goods_sum[$key]['num']);
            } else {
                $num = 0;
            }
            $goods_category[$key]['goods_num'] = $num;
        }

        Response::assign('goods_category', $goods_category);
        Response::display('admin/category_list.html');
    }

    public function edit() {
        $id = an_request('id');

        $info = array();
        if ($id) {
            $info = _uri('goods_category', $id);
        }

        Response::assign('info', $info);
        Response::display('admin/category_edit.html');
    }

    public function save() {
        $param = an_request('data', array());

        if ($param['title'] == '') {
            return '请填写分类名称';
        }

        $data = array();
        $data['title'] = $param['title'];

        $info = _uri('goods_category', $param['id']);
        if ($info) {
            $data['update_time'] = datetime();
            $ret = _model('goods_category')->update($info['id'], $data);
        } else {
            $data['add_time'] = datetime();
            $ret = _model('goods_category')->create($data);
        }

        if ($ret) {
            return array('保存成功', 'success', AnUrl('goods/admin/category'));
        } else {
            return '保存失败';
        }
    }

    public function change_status() {
        $status = an_request('status');
        $id = an_request('id');

        if ($status > -1 && $id > 0) {
            $ret = _model('goods_category')->update($id, array('status' => $status));
            if ($ret) {
                return array('更新成功', 'success', AnUrl('goods/admin/category'));
            }
        }
        return '更新失败';
    }
}

?>